package data_structure.helper;

import data_structure.mooc.*;
import data_structure.tensort.Sort;

public class SortingHelper {

    private SortingHelper() {
    }

    /**
     * check whether to sort the array
     *
     * @param array
     * @param <E>
     * @return
     */
    public static <E extends Comparable<E>> boolean isSorted(E[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            if (array[i].compareTo (array[i + 1]) > 0) {
                return false;
            }
        }
        return true;
    }

    /**
     * to sort the given array
     * out the result:the array been sorted and the time to sort the given array
     *
     * @param array       target array
     * @param sortingName the name of sort method
     * @param <E>
     */
    public static <E extends Comparable<E>> void testSorting(E[] array, String sortingName) {

        long start = System.nanoTime ();
        if (sortingName.equals ("selectionSort")) {
            array = SelectionSort.sort (array);
        }
        if (sortingName.equals ("insertionSort")) {
            array = InsertionSort.sort2 (array);
        }
        if (sortingName.equals ("mergeSort")) {
            MergeSort.sort (array);
        }
        if (sortingName.equals ("QuickSort1")) {
            QuickSort.sort (array);
        }
        if (sortingName.equals ("QuickSort")) {
            QuickSort.sort2Ways (array);
        }
        if (sortingName.equals ("quickSort3")) {
            QuickSort.sort3Ways (array);
        }
        if (sortingName.equals ("heapSort")) {
            HeapSort.sort (array);
        }
        if (sortingName.equals ("BubbleSort")) {
            BubbleSort.sort (array);
        }
        if (sortingName.equals ("BubbleSort2")) {
            BubbleSort.sort2 (array);
        }
        if (sortingName.equals ("BubbleSort3")) {
            BubbleSort.sort3 (array);
        }
        if (sortingName.equals ("ShellSort")) {
            ShellSort.sort (array);
        }
        if (sortingName.equals ("LSDSort")) {

            String[] str = (String[]) array;
            if (str.length == 0) {
                throw new NullPointerException ("array is empty");
            }
            LSDSort.sort (str, str[0].length ());
        }
        if(sortingName.equals("MSDSort")){
            String [] str=(String[])array;
            MSDSort.sort (str);
        }
        if(sortingName.equals ("BucketSort")){

            Integer []arr=(Integer[])array;
            BucketSort.sort2 (arr,20);
        }
        long end = System.nanoTime ();
        if (!isSorted (array)) {
            throw new RuntimeException ("sort failed");
        }
//        for(int i=0;i<array.length;i++){
//            System.out.print(array[i]);
//            System.out.print(" ");
//        }
//        System.out.println();
        System.out.printf ("array number=%d time: %f method: %s", array.length, (end - start) / 1000000000.0, sortingName);
        System.out.println ();
    }
}
